package com.tang.realm;

import com.tang.mapper.UserInfoMapper;
import com.tang.pojo.SysPermission;
import com.tang.pojo.SysRole;
import com.tang.pojo.UserInfo;
import com.tang.pojo.UserInfoExample;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class MyShiroRealm extends AuthorizingRealm {

    @Autowired
    private UserInfoMapper userInfoMapper;



    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("=====权限配置=====");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        UserInfo userInfo  = (UserInfo)principals.getPrimaryPrincipal();

        //获得当前subject 所对应的角色，并添加到权限配置中去
        System.out.println("角色：");
        List<SysRole> sysRoleList = userInfoMapper.selectRoleListByUid(userInfo.getUid());
        for(SysRole role:sysRoleList){
            System.out.println(role.getRole());
            info.addRole(role.getRole());
        }

        List<SysPermission> sysPermissionList = userInfoMapper.selectPermissionListByUid(userInfo.getUid());
        System.out.println("权限：");
        for(SysPermission permission:sysPermissionList){
            System.out.println(permission.getPermission());
            info.addStringPermission(permission.getPermission());
        }

        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("=====登录认证=====");
        //获取用户的输入的账号.
        String username = (String)token.getPrincipal();
        System.out.println(token.getCredentials());
        //通过username从数据库中查找 User对象，如果找到，没找到.
        //实际项目中，这里可以根据实际情况做缓存，如果不做，Shiro自己也是有时间间隔机制，2分钟内不会重复执行该方法
        UserInfoExample example=new UserInfoExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<UserInfo> userInfo_list = userInfoMapper.selectByExample(example);

        if(userInfo_list==null) {
            return null;
        }
        UserInfo userInfo=userInfo_list.get(0);
        System.out.println("userInfo:"+userInfo.getName()+","+userInfo.getUsername());





        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                userInfo, //用户名
                userInfo.getPassword(), //user.getPassword()，注意这里是指从数据库中获取的password
                //ByteSource.Util.bytes(userInfo.getCredentialsSalt()),//salt=username+salt
                getName()  //第三个字段是realm，即当前realm的名称
        );
        return authenticationInfo;
    }
}
